#include "buttomwgt.h"
#include <QHBoxLayout>
#include <QDebug>
#include <QVBoxLayout>

//时间格式化，视频时间单位是毫秒，00:00:00
static QString millisecondsToTimeFormat(qint64 millseconds)
{
    qint64 seconds = millseconds / 1000;
    qint64 minutes = seconds / 60;
    qint64 hours = minutes / 60;
    seconds %= 60;
    minutes %= 60;
    return QString("%1:%2:%3:").arg(hours,2,10,QChar('0'))
            .arg(minutes,2,10,QChar('0'))
            .arg(seconds,2,10,QChar('0'));
}

Buttomwgt::Buttomwgt(QWidget *parent) : QWidget(parent)
{
    setFixedSize(900,60);
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_StyledBackground);

    //创建变量
    curTime = new QLabel("00:00:00",this);
    totalTime = new QLabel("00:00:00",this);
    flagLabel = new QLabel("/",this);

    preBtn = new QPushButton(this);
    nextBtn = new QPushButton(this);
    playBtn = new QPushButton(this);
    stopBtn = new QPushButton(this);
    settingBtn = new QPushButton(this);
    playRationBtn = new QPushButton(this);
    moreVediaBtn = new QPushButton(this);
    volumBtn = new QPushButton(this);

    volumSlider = new QSlider(this);
    progressSlider = new QSlider(this);

    //滑动条设置方向，水平方向
    volumSlider->setOrientation(Qt::Horizontal);
    progressSlider->setOrientation(Qt::Horizontal);

    //控件大小
    volumBtn->setFixedSize(25,25);
    volumSlider->setFixedSize(170,25);
    progressSlider->setFixedSize(25,25);
    playBtn->setFixedSize(25,25);
    stopBtn->setFixedSize(25,25);
    preBtn->setFixedSize(25,25);
    nextBtn->setFixedSize(25,25);
    curTime->setFixedSize(56,25);
    flagLabel->setFixedSize(10,25);
    totalTime->setFixedSize(56,25);
    settingBtn->setFixedSize(30,25);
    moreVediaBtn->setFixedSize(21,21);
    playRationBtn->setFixedSize(40,25);

    //
    QWidget* subWidget1 = new QWidget(this);
    subWidget1->setFixedSize(900,25);
    QHBoxLayout* layout = new QHBoxLayout(subWidget1);
    layout->setSpacing(0);
    layout->addWidget(progressSlider);
    layout->addWidget(volumBtn);
    layout->addWidget(volumSlider);
    layout->setContentsMargins(0,0,0,0);
    subWidget1->setLayout(layout);

    QWidget* subWidget2 = new QWidget(this);
    subWidget2->setFixedSize(900,25);
    QHBoxLayout* layout2 = new QHBoxLayout(subWidget2);
    layout2->setSpacing(0);
    layout2->addWidget(playBtn);
    layout2->addWidget(stopBtn);
    layout2->addWidget(preBtn);
    layout2->addWidget(nextBtn);
    layout2->addWidget(playRationBtn);
    layout2->addWidget(curTime);
    layout2->addWidget(flagLabel);
    layout2->addWidget(totalTime);
    layout2->addStretch();
    layout2->addWidget(settingBtn);
    layout2->addWidget(moreVediaBtn);
    layout2->setContentsMargins(0,0,0,0);
    subWidget2->setLayout(layout2);

    QVBoxLayout*vlayout = new QVBoxLayout(this);
    vlayout->setSpacing(0);
    vlayout->addWidget(subWidget1);
    vlayout->addWidget(subWidget2);
    vlayout->setContentsMargins(0,0,0,0);
    this->setLayout(vlayout);

    RegisterSignal();
}

void Buttomwgt::InitMediaData(const QVariantMap &data)
{
    if(data.isEmpty())
    {
        return;
    }
    int total = data["duration"].toInt();
    progressSlider->setRange(0,total* 100);
    progressSlider->setSingleStep(1);
    progressSlider->setPageStep(100);
    totalTime->setText(millisecondsToTimeFormat(total));
    //保存总时长消息
    totalTime->setProperty("alltime",total);
}

void Buttomwgt::UpdataSlider(int time)
{
    curTime->setText(millisecondsToTimeFormat(time));
    //更新进度条
    double rate = static_cast<double>(time) / static_cast<double>(totalTime->property("alltime").toInt());
    qint64 curRatetime = static_cast<qint64>(rate * progressSlider->maximum());
    progressSlider->setValue(curRatetime);
}

void Buttomwgt::RegisterSignal()
{

}

void Buttomwgt::MediaControlHandle(const QString &type, const QVariant &value)
{

}
